package base.thread.threadCommunication.guanChengFa;

/**
 * @Author: my_code
 * @Create: 2020-12-09 13:30
 */
public class SynContainer {

    Cake[] cakes = new Cake[10];
    int count = 0;

    public synchronized void push(Cake cake){

        if (count == cakes.length){
            //生产者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        cakes[count] = cake;
        count++;
        this.notifyAll();
    }

    public synchronized Cake pop(){

        if(count == 0){
            //让消费线程等待，唤醒生产者生成
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Cake cake = cakes[count];
        this.notifyAll();
        return cake;
    }
}
